package ldkxingzhe.l365calendar.fragment;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import ldkxingzhe.l365calendar.R;
import ldkxingzhe.l365calendar.activity.AllEditActivity;
import ldkxingzhe.l365calendar.fragment.presenter.ScheduleAlarmPresenter;
import ldkxingzhe.l365calendar.utils.OSUtil;

/**
 * 提醒设置界面
 * Created by ke on 16-1-30.
 */
public class ScheduleAlarmFragment extends AllEditFragmentBase
        implements ScheduleAlarmPresenter.PView, AdapterView.OnItemClickListener, View.OnClickListener {
    @SuppressWarnings("unused")
    private final static String TAG = "ScheduleAlarmFragment";

    public static final String IS_ALL_DAY = "schedule_alarm_is_all_day";
    public static final String START_TIME = "schedule_alarm_start_time";
    public static final String POSITION_ARRAY = "schedule_alarm_position_array";
    public static final String ALL_DAY_ALARM_TIME_OFFSET = "all_day_alarm_time_offset";
    public static final String RESULT = "result";

    // UI Element
    private GridView gridView;
    private LinearLayout alarmDescLayout;
    private TextView hint;

    private ScheduleAlarmPresenter presenter;
    private AlarmBaseAdapter adapter;


    public static ScheduleAlarmFragment newInstance(Bundle bundle) {
        ScheduleAlarmFragment fragment = new ScheduleAlarmFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        presenter = new ScheduleAlarmPresenter();
        presenter.setView(this);
        presenter.onCreate(getArguments());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View resultView = inflater.inflate(R.layout.fragment_schedule_alarm, container,false);
        gridView = (GridView) resultView.findViewById(R.id.grid_view);
        alarmDescLayout = (LinearLayout) resultView.findViewById(R.id.alarm_desc_layout);
        hint = (TextView) resultView.findViewById(R.id.hint);
        alarmDescLayout.setVisibility(View.VISIBLE);
        adapter= new AlarmBaseAdapter();
        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener(this);
        hint.setOnClickListener(this);
        presenter.onCreateView();
        return resultView;
    }
    @Override
    public void alarmDescLayoutAddView(String name, String value){
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.item_left_name_right_value, alarmDescLayout, false);
        TextView leftName = (TextView) view.findViewById(R.id.name);
        TextView rightValue = (TextView) view.findViewById(R.id.value);
        leftName.setText(name);
        rightValue.setText(value);
        alarmDescLayout.addView(view);
    }


    @Override
    public void alarmDescLayoutClear() {
        alarmDescLayout.removeAllViews();
    }

    @Override
    public void setHintText(String text) {
        hint.setText(text);
    }

    @Override
    public void setHintVisibility(int visibility) {
        hint.setVisibility(View.VISIBLE);
    }

    @Override
    public void onSaveClick() {
        Intent intent = new Intent();
        intent.putIntegerArrayListExtra(RESULT, presenter.getSelectPosition());
        getActivity().setResult(Activity.RESULT_OK, intent);
        getActivity().finish();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        presenter.onItemClick(position);
        setIsChanged(true);
    }

    @Override
    public void notifyDateSetChanged() {
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(getActivity(), AllEditActivity.class);
        intent.putExtra(AllEditActivity.TYPE, AllEditActivity.SINGLE_TAB);
        intent.putExtra(AllEditActivity.TITLE_NAME, getString(R.string.all_day_alarm_time));
        intent.putExtra(AllEditActivity.CLASS_INDEX, 3);
        intent.putExtra(ScheduleAlertSetupFragment.TIME_LONG, presenter.getAllDayAlarmOffsetTime());
        startActivityForResult(intent, 0);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode == Activity.RESULT_CANCELED) return;
        presenter.onAllDayAlertTimeChanged(resultCode, data);
    }

    class AlarmBaseAdapter extends BaseAdapter{

        private int cellWidth = -1;

        @Override
        public int getCount() {
            return presenter.getCount();
        }

        @Override
        public String getItem(int position) {
            return presenter.getItemName(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if(convertView == null){
                convertView = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.grid_view_category, parent, false);
                viewHolder = new ViewHolder();
                viewHolder.textView = (TextView) convertView.findViewById(R.id.text);
                viewHolder.imageView = (ImageView) convertView.findViewById(R.id.image);
                if(cellWidth == -1){
                    cellWidth = (OSUtil.getScreenWidth() - 100) / gridView.getNumColumns();
                }
                convertView.setMinimumHeight(cellWidth);
                convertView.setTag(viewHolder);
            }else{
                viewHolder = (ViewHolder) convertView.getTag();
            }

            viewHolder.textView.setText(getItem(position));
            if(presenter.isSelected(position)){
                convertView.setBackgroundColor(Color.GRAY);
                viewHolder.textView.setTextColor(Color.BLUE);
            }else {
                viewHolder.textView.setTextColor(Color.parseColor("#ff888e92"));
                convertView.setBackgroundColor(Color.WHITE);
            }
            return convertView;
        }

        class ViewHolder{
            TextView textView;
            ImageView imageView;
        }
    }
}
